from queue import Queue
from threading import Lock
from prg.entity.task_entity import TaskEntity
import uuid


class PublicEntity:
    _instance_lock = Lock()

    def __init__(self):
        self.command = Queue()
        self.handle = Queue()
        self.addr = dict()
        self.port = Queue()
        self.udp = dict()
        self.task = dict()
        self.server = None

    def get_task(self, task_id=None):
        if task_id is None:
            task_id = str(uuid.uuid1())
        if task_id not in self.task:
            self.task[task_id] = TaskEntity()
        return task_id, self.task[task_id]

    @staticmethod
    def instance():
        if not hasattr(PublicEntity, "_instance"):
            with PublicEntity._instance_lock:
                if not hasattr(PublicEntity, "_instance"):
                    PublicEntity._instance = PublicEntity()
        return PublicEntity._instance
